package com.team.controller;

import com.team.config.AlipayConfig;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.team.config.AlipayConfig;
import com.team.entity.Reservation;
import com.team.service.AliPayService;
import com.team.util.WebUtil;
import com.team.vo.AppointmentMessage;
import com.team.vo.PersonalInformation;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author keluoyisi
 * @program: seven
 * @description: 支付宝沙箱支付的接口
 * @date 2022-10-21 18:30:22
 */
@Controller
@RequestMapping("/AliPay")
public class AliPayController {
    /**
     * 引入一些配置信息
     */
    private static final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "UTF-8";
    /**
     * 引入service
     */
    @Resource(name = "aliPayService")
    private AliPayService aliPayService;
    //签名方式
    private static final String SIGN_TYPE = "RSA2";

    /**
     * 沙箱支付的支付请求控制器
     * @param
     * @return
     */
    @RequestMapping("/Pay")
    public void orderPay(HttpServletResponse response){
        AppointmentMessage appointmentMessage = (AppointmentMessage) WebUtil.getSession().getAttribute("appointmentMessage");
        Reservation reservation = (Reservation) WebUtil.getSession().getAttribute("reservation");
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no =reservation.getPnum();
        // 订单名称，必填
        String subject = reservation.getName()+"挂号";
        // 付款金额，必填
        String total_amount=appointmentMessage.getPrice()+"";
        // 超时时间 可空
        String timeout_express="2m";
        // 销售产品码 必填
        String product_code="QUICK_WAP_WAY";
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(AlipayConfig.notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(AlipayConfig.return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 沙箱支付的回调控制器
     * @param request 请求
     * @param response 相应
     * @return 跳转页面
     * @throws Exception 异常
     */
    @RequestMapping ("/Notify")  // 注意这里必须是POST接口
    public void payNotify(HttpServletRequest request,HttpServletResponse response) throws Exception {
        boolean b = aliPayService.orderPayConf(response, request);
        response.sendRedirect("/static/reservation.jsp");
    }
}
